type LogLevel = 'info' | 'error' | 'warning' | 'success';

const prettyLog = () => {
    const isProduction = import.meta.env.NODE_NAME === 'production'; // 根据环境变量判断是否为生产环境

    // 判断值是否为空
    const isEmpty = (value: any): boolean => {
        return value == null || value === undefined || value === '';
    };

    // 美化打印
    const prettyPrint = (title: string, text: string, color: string): void => {
        if (isProduction) return;
        console.log(
            `%c ${title} %c ${text} %c`,
            `background:${color};border:1px solid ${color}; padding: 1px; border-radius: 2px 0 0 2px; color: #fff;`,
            `border:1px solid ${color}; padding: 1px; border-radius: 0 2px 2px 0; color: ${color};`,
            'background:transparent'
        );
    };

    // 通用日志方法
    const log = (level: LogLevel, textOrTitle: string, content: string = ''): void => {
        const defaultTitles = {
            info: 'Info',
            error: 'Error',
            warning: 'Warning',
            success: 'Success',
        };
        const title = isEmpty(content) ? defaultTitles[level] : textOrTitle;
        const text = isEmpty(content) ? textOrTitle : content;
        const colors = {
            info: '#909399',
            error: '#F56C6C',
            warning: '#E6A23C',
            success: '#67C23A',
        };
        prettyPrint(title, text, colors[level]);
    };

    // 打印表格
    const table = (data: Array<Record<string, any>> = []): void => {
        if (isProduction) return;

        // 打印表头
        const headers = Object.keys(data[0] || {});
        console.log(
            `%c ${headers.join('%c ')}`,
            ...headers.map(() => 'color: white; background-color: black; padding: 2px 10px;')
        );

        // 打印表格数据
        data.forEach((row) => {
            console.log(
                `%c ${Object.values(row).join('%c ')} %c`,
                ...Object.values(row).map(() => 'color: black; background-color: lightgray; padding: 2px 10px;')
            );
        });
    };

    // 打印图片
    const picture = (url: string, scale: number = 1): void => {
        if (isProduction) return;

        const img = new Image();
        img.crossOrigin = 'anonymous';
        img.onload = () => {
            console.log(
                `%c sup?`,
                `font-size: 1px;
                padding: ${Math.floor((img.height * scale) / 2)}px ${Math.floor((img.width * scale) / 2)}px;
                background-image: url(${url});
                background-repeat: no-repeat;
                background-size: ${img.width * scale}px ${img.height * scale}px;
                color: transparent;`
            );
        };
        img.src = url;
    };

    return {
        info: (textOrTitle: string, content?: string) => log('info', textOrTitle, content),
        error: (textOrTitle: string, content?: string) => log('error', textOrTitle, content),
        warning: (textOrTitle: string, content?: string) => log('warning', textOrTitle, content),
        success: (textOrTitle: string, content?: string) => log('success', textOrTitle, content),
        table,
        picture,
    };
};

// 创建打印对象
const log = prettyLog();

// 示例用法
log.info('This is an info message');
log.error('This is an error message');
log.warning('This is a warning message');
log.success('This is a success message');

log.table([
    { id: 1, name: 'Alice', age: 25 },
    { id: 2, name: 'Bob', age: 30 },
    { id: 3, name: 'Charlie', age: 35 },
]);

log.picture('https://example.com/image.png', 0.5);